

import pygame,string,random, time
import sys
from pygame.locals import *
import os

# inicializar o pygame
pygame.init()

# definir o tamanho da janela
janela = pygame.display.set_mode((600 ,480 ))

# O nome do jogo
pygame.display.set_caption('METAL MEMORY')
menu = pygame.image.load("fundo.jpg").convert_alpha()
tela = pygame.image.load("tela.jpg").convert_alpha()
cred = pygame . image . load ( "cred.jpg " ).convert_alpha()
tb = pygame . image . load ( "tabela.png " ).convert_alpha()
erro = pygame.image.load("erro.png").convert_alpha()
ganhou = pygame.image.load("ganhou.jpg").convert_alpha() 
fundo = menu
#sons
som = pygame.mixer.Sound("son.wav")
som1 = pygame.mixer.Sound("som.wav")
janela . fill ( ( 0 , 0 , 0 ) )
janela . blit ( menu , menu . get_rect ())

# Menu
inicio = True
while inicio:
    # inicio do evento Menu
    instrucoes = False
    voltar = False
    sair = False
    iniciar = False
    for event in pygame . event .get (): 
         if event.type is MOUSEBUTTONDOWN:
             x,y = event.pos
             iniciar = x > 412 and x < 506 and y > 164 and y < 186
             instrucoes = x > 437 and x < 597 and y > 239 and y < 260
             voltar =  x > 438 and x < 528 and y > 415 and y < 424 
             sair = x > 491 and x < 544 and y > 313 and y < 334
               
    som1.play()
    if instrucoes:
        fundo = cred

    if voltar:
        fundo = menu

    if iniciar:
        som1.stop()
        inicio = False
                
    if sair:
        pygame.quit()
        sys.exit()

    janela.blit(fundo,(0,0))
    pygame.display.update()
som.play()    
local = [(50,150), (160,150),(270,150), (50,250),(160,250),(270,250)]
random.shuffle(local)
fundo = tela

p = []
p1 = []
for x in range(1,7):
    img  = pygame.image.load("gt/" +str(x) + ".jpg")
    p.append(img)
    
janela.blit(fundo, (0,0))

for i in range(6):
    janela.blit(p[i], local[i])
pygame.display.update()

time.sleep(5)


local1 = [(480,40),(480,105),(480,170),(480,235),(480,300),(480,365)]

for x in range(1,7):
    img  = pygame.image.load("gr/" +str(x) + ".jpg")
    p1.append(img)
jogar = True
 
selecionado0 = None
selecionado1 = None
selecionado2 = None
selecionado3 = None
selecionado4 = None
selecionado5 = None
usados = []
def remover_do_local(lista, pos):
    for i in lista:
        if i[1] == pos:
            lista.remove(i)
            break

verificador = 0       
while jogar: 
    for event in pygame . event .get ():
        janela.blit(fundo, (0,0))
        for i in range(6):
            janela.blit(p1[i], local1[i])
            janela.blit(tb, (51,151))
        for i, pos in usados:
            janela.blit(i, pos)
        selects = []
        if event.type is MOUSEBUTTONDOWN:
             x,y = event.pos
             if x > 480 and x < 580 and y > 40 and y < 100:
                selecionado0 = 0
             elif x > 480 and x < 580 and y > 105 and y < 165:
                selecionado1 = 1
             elif x > 480 and x < 580 and y > 170 and y < 230:
                selecionado2 = 2
             elif x > 480 and x < 580 and y > 235 and y < 295:
                selecionado3 = 3
             elif x > 480 and x < 580 and y > 300 and y < 360:
                selecionado4 = 4
             elif x > 480 and x < 580 and y > 365 and y < 425:
                selecionado5 = 5
            
             

        if event.type is KEYDOWN:
            
            if event.key == K_1 and selecionado0 != None:
                remover_do_local(usados, (50,150))
                usados.append((p[selecionado0],(50, 150)))
            elif event.key == K_2 and selecionado1 != None:
                remover_do_local(usados, (160,150))
                usados.append((p[selecionado1],(160, 150)))
            elif event.key == K_3 and selecionado2 != None:
                remover_do_local(usados, (270,150))
                usados.append((p[selecionado2],(270, 150)))
            elif event.key == K_4 and selecionado3 != None:
                remover_do_local(usados, (50,250))
                usados.append((p[selecionado3],(50, 250)))
            elif event.key == K_5 and selecionado4 != None:
                remover_do_local(usados, (160,250))
                usados.append((p[selecionado4],(160, 250)))
            elif event.key == K_6 and selecionado5 != None:
                remover_do_local(usados, (270,250))
                usados.append((p[selecionado5],(270, 250)))

    
    
    print usados    
    pygame.display.update()
    




    
        



















                 
